package com.stadium.web.controller;

import com.fastsite.common.api.ApiException;
import com.fastsite.common.api.ApiResult;
import com.fastsite.common.api.ApiSecured;
import com.fastsite.common.enums.StatusEnum;
import com.fastsite.common.group.Group;
import com.fastsite.utils.validator.ValidatorUtils;
import com.stadium.web.domain.Region;
import com.stadium.web.domain.Venue;
import com.stadium.web.service.RegionService;
import com.stadium.web.service.VenueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Authorization;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;


/**
 * 区域信息对象 region
 *
 * @author ruoyi
 * @date 2020-03-15
 */
@Api(tags = "区域信息管理")
@RestController
@RequestMapping("/web/region")
public class RegionController {

    @Autowired
    private RegionService regionService;

    @Autowired
    private VenueService venueService;
    /**
     * 列表页面
     */
    @ApiOperation("区域信息列表信息")
    @ApiImplicitParams({
    @ApiImplicitParam(name="page",value="当前页数",required=false,paramType="form",dataType="Long"),
    @ApiImplicitParam(name="size",value="每页的数量",required=false,paramType="form",dataType="Long"),
//    @ApiImplicitParam(name="id",value="主键",required=false,paramType="form",dataType="Long"),
    @ApiImplicitParam(name="name",value="区域名称",required=false,paramType="form",dataType="String"),
    })
    @GetMapping("/list")
    public ApiResult index(@ApiIgnore Region region){

        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("name",match-> match.contains());
        region.setStatus(StatusEnum.OK.getCode());
        // 获取区域信息列表
        Example<Region> example = Example.of(region, matcher);
        Page<Region> list = regionService.getPageList(example);

        HashMap<String,Object> result = new HashMap<>();
        result.put("list", list.getContent());
        result.put("page", list);
        // 封装数据
        return ApiResult.ok(result,"获取区域信息列表信息");
    }

    /**
     * 跳转到编辑页面
     */
    @ApiOperation("单个区域信息信息")
    @GetMapping("/getOneRegion")
    public ApiResult getOneRegion(@RequestParam("id") Long id){
        Region region=  regionService.getById(id);
        return ApiResult.ok(region,"获取单个区域信息");
    }

    @ApiOperation("获取全部区域")
    @GetMapping("/getAllRegion")
    public ApiResult findAll(){
        // 封装数据
        return ApiResult.ok(regionService.findAllRegion(),"获取区域信息列表信息");
    }

    /**
     * 获取区域场馆
     */
    @ApiOperation("获取区域场馆")
    @GetMapping("/getVenueByRegionId")
    public ApiResult getVenueByRegionId(@RequestParam("regionId") Long regionId){
        List<Venue> venues=  venueService.getVenueByRegionId(regionId);
        return ApiResult.ok(venues,"区域场馆信息");
    }
    /**
     * 保存添加/修改的数据
     * @param regionFrom 实体对象
     */
    @ApiSecured
    @ApiOperation(value = "保存/修改单个区域信息", authorizations = @Authorization(value = "token"), produces =  MediaType.APPLICATION_JSON_UTF8_VALUE)
//    @ApiImplicitParams({
//    @ApiImplicitParam(name="id",value="主键",required=false,paramType="body",dataType="Long"),
//    @ApiImplicitParam(name="name",value="区域名称",required=false,paramType="body",dataType="String"),
//    })
    @PostMapping({"/save","/update"})
    public ApiResult save(  @RequestBody Region regionFrom){
        Region region = null;
        Boolean result = regionService.exitName(regionFrom.getName(),regionFrom.getId());
        if(result){
            throw new ApiException("该区域名称以存在");
        }
        if(!StringUtils.isEmpty(regionFrom.getId())){
            //修改
            region = regionService.getById(regionFrom.getId());

            region.setId(regionFrom.getId());
                region.setName(regionFrom.getName());
            }else{
            //新增
            region = new Region();
            region.setStatus(StatusEnum.OK.getCode());
            BeanUtils.copyProperties(regionFrom,region);
        }
        // 保存数据
        regionService.save(region);
        return ApiResult.ok("保存成功");
    }


    /**
     * 设置一条或者多条数据的状态
     */
    @ApiSecured
    @ApiOperation(value = "修改区域信息状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name="param",value="状态（1:正常，2:冻结,3:删除）",required=true,paramType="path",dataType="String"),
            @ApiImplicitParam(name="ids",value="修改[id]",required=true,paramType="form",dataType="List")}
    )
    @RequestMapping(value = "/status/{param}",method={RequestMethod.GET,RequestMethod.POST})
    public ApiResult status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<Long> ids){
        // 更新状态
        StatusEnum statusEnum = StatusEnum.getByCode(Byte.parseByte(param));
        if (regionService.updateStatus(statusEnum, ids)) {
            return ApiResult.ok(statusEnum.getMessage() + "成功");
        } else {
            return ApiResult.err(statusEnum.getMessage() + "失败，请重新操作");
        }
    }
}

